package com.sc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class BaseController {

	public HttpSession getSession(HttpServletRequest req) {
		return req.getSession();
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public Object getSessionAttribute(String arg) {
		return getSession().getAttribute(arg);
	}

	public void setSessionAttribute(String arg, Object obj) {
		getSession().setAttribute(arg, obj);
	}

	public String getUserId() {
		 return (String) getSession().getAttribute("userId");
		//return "1";
	}

	public boolean isLogin() {
		return StringUtils.isNotBlank(getUserId());
	}

	/**
	 * 当文件上传时,取到的request不是同一个,应该传入参数
	 * 
	 * @return
	 */
	public HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	public HttpServletResponse getResponse() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
	}

}
